package com.yuanye.gwcit.fragments;

import com.yuanye.gwcit.GW;
import com.yuanye.yeecho.base.CommonTaskFragment;
import com.yuanye.yeecho.utils.YY;

import java.util.Arrays;
import java.util.List;

public class EmmcFragment extends CommonTaskFragment {

    public EmmcFragment() {
        setTaskName("EMMC检测");
    }

    @Override
    public void execTask() {
        super.execTask();
        String emmc = YY.execShell("getprop ro.emmc.name");
        String stdEmmc = GW.cfgPcba.getEmmcModel();
        List<String> list = Arrays.asList(stdEmmc.split("#"));
        appendLog("EMMC信息：" + emmc);
        appendLog("参考信息：" + list.toString());

        try {
            //分解size大小
//        String modelStr = emmc.split(",")[0];
            String sizeStr = emmc.split(",")[1];
            sizeStr = sizeStr.substring(0, sizeStr.length()-3);
            double size = Double.parseDouble(sizeStr);
            //获取本地size大小
            String[] cmds = {"sh", "-c", "cat /proc/partitions | awk '/mmcblk2/ {print$3}' | head -n 1"};
            String temp = YY.execShell(cmds);
            double localEmmcSize = Double.parseDouble(temp) / 1024 /1024;
            localEmmcSize = Math.round(localEmmcSize) * 10 / 10;
            appendLog("本地EMMC大小："+localEmmcSize+"GiB");
            if (!emmc.isEmpty() && list.contains(emmc)){
                double deltaSize = Math.abs(size - localEmmcSize);
                if (deltaSize <= 3){
                    appendLog("EMMC检测通过");
                    passAndNext();
                }else{
                    appendLogF("公差过大");
                    setFail();
                }

            }else{
                appendLogF("比对出错");
                setFail();
            }
        }catch (Exception e){
            e.printStackTrace();
            appendLogF("获取EMMC数据出错");
            appendLogF(e.toString());
            setFail();
        }
    }
}
